#!/bin/bash

#强制执行bash
#/bin/bash

#生效环境变量
source /etc/profile
source /root/.bash_profile

#!/bin/bash
BACKUP_PATH=/var/discuz_deploy/db_bak
MYSQL_DUMP="docker-compose exec mysql mysqldump"
BACKUP_TIME=$(date +%Y%m%d.%H%M)
BACKUP_DB=discuz
DB_USER=root
DB_PAWD=discuz

cd /var/discuz_deploy/

${MYSQL_DUMP} -u${DB_USER} -p${DB_PAWD} ${BACKUP_DB} > ${BACKUP_PATH}/${BACKUP_DB}.sql
if [ $? -ne 0  ];then
    echo "${BACKUP_TIME} mysqldump error " >>  ${BACKUP_PATH}/dump.log
    exit 1
fi

tar -zcvf ${BACKUP_PATH}/${BACKUP_DB}.${BACKUP_TIME}.tar.gz ${BACKUP_PATH}/${BACKUP_DB}.sql 
rm -f ${BACKUP_PATH}/${BACKUP_DB}.sql 

#删除10天前的文件
find ${BACKUP_PATH} -name "*.tar.gz" -mtime +10 -exec rm -rfv {} \;
exit 0